<?php
namespace console\controllers;

use yii;
use yii\console\Controller;
use backend\models\Contract;

class FixHelperController extends Controller
{    
    public function actionAddRegistryNumber()
    {
        //получить все объекты
        $contractArr = Contract::find()->asArray()->all();

        //перебрать все объекты
        foreach($contractArr as $contract)
        {
            if($contract['registry_number'])
            {
                echo $contract['subject'] . PHP_EOL;
                echo 'имее реестровый номер'. PHP_EOL;
            }
            else
            {   
                //записать эту информацию в бд
                $model = Contract::findOne(['id' => $contract['id']]);
                $model->registry_number = Contract::generateRegistryNumber();
                $model->save();
                
                echo $contract['subject'] . PHP_EOL;
                echo 'реестровый номер ' . $model->registry_number . ' сгенерирован'. PHP_EOL;
                sleep(1);
            }
        }

    }

    public function actionCheckCommonImage()
    {
        $s3 = Yii::$app->get('s3');
        $result = $s3->commands()->list('')->inBucket('ogikm-img')->execute();

        foreach($result['Contents'] as $resultImage)
        {
            if(! \backend\models\CommonImage::findOne(['filename' => $resultImage['Key']]))
            {
                $imageData = getimagesize('https://storage.yandexcloud.net/' . \backend\models\CommonImage::BUCKET_NAME . '/' . $resultImage['Key']);
                
                $model = new \backend\models\CommonImage;
                $model->filename = $resultImage['Key'];
                $model->size_px = $imageData[0] . 'x' .  $imageData[1];
                $model->size_b = $resultImage['Size'];
                $model->save();
                echo $resultImage['Key'] . PHP_EOL;
                sleep(1);
            }
        }
    }
}